<?php
/**
 * Created by PhpStorm.
 * User: qiaoli
 * Date: 15/11/2
 * Time: 下午5:01
 */
defined('BASEPATH') OR exit('No direct script access allowed');
class Sendmsgm extends CI_Model
{
    const MSG_APP_TYPE_SYSTEM = 1;
    const MSG_APP_TYPE_PERSONAL = 2;
    const MSG_APP_TYPE_ATTENTION = 3;
    const MSG_APP_TYPE_INVITATION = 4;
    const MSG_APP_TYPE_CHAT = 5;

    const CHAT_NOTICE_GROUP = 1;
    const CHAT_NOTICE_AUDIT = 2;


    function __construct()
    {
        $this->load->model('postm');
        $this->load->library('curl');
    }
    function send_message($post_id,$receive_uid,$content)
    {
        $this->send_message_full($post_id,$receive_uid,$content,$this->uid);
    }

    function send_message_full($post_id,$receive_uid,$content,$from_uid, $apptype=self::MSG_APP_TYPE_PERSONAL)
    {
        //发消息
        $this->load->library('curl');

        $message_url = MESSAGE_URL.'v1/sendmsg.json';

        $post_first_img = $this->postm->get_post_first_img($post_id);
        $post_first_img = ret_fullpath_url($post_first_img);
        $send_message_array = array();

        $send_message_array['post_first_img'] = $post_first_img;
        $send_message_array['post_id'] = (string)$post_id;

        $send_message_array['content'] = $content;
        $send_message_string = json_encode($send_message_array);
        $post_message_data = array();
        $post_message_data['fromuid'] = $from_uid; //发送人UID
        $post_message_data['touids'] = $receive_uid; //接受人UID
        $post_message_data['message'] = $send_message_string;   //消息体

        $post_message_data['type'] = 0;             //（系统消息则type为0）
        $post_message_data['apptype'] = $apptype;   //分配的apptype
        $post_message_data['msgtype'] = 1;          //消息类型（默认为1）
        $post_message_data['subject'] = "";         //消息主题（可为空）
        $post_message_data['fromusername'] = "";    //发送者名称（可为空）
        $post_message_data['op'] = 'sendpm';        //发送类型（必须为sendpm）
        //开始发送消息
        $ret = $this->curl->simple_post($message_url,$post_message_data);
        return $ret;
    }

    public function update_msg_content($msg_id, $message_array)
    {
        $this->load->library('curl');

        $message_url = MESSAGE_URL.'v1/editmsg.json';

        $message_data = array("msg_id"=> $msg_id);
        $message_data["message"] = json_encode($message_array);

        $ret = $this->curl->simple_post($message_url,$message_data);

        return $ret;
    }


    public function build_group_chat_msg($icon, $title, $body, $gid, $from, $apply_status) {
        return array(
            "icon"=> $icon,                        // 左侧 icon
            "title"=> $title,                      // 上方标题
            "body"=> $body,                        // 中部消息
            "gid"=> $gid,                          // 群 id
            "from"=> $from,                        // 来自
            "apply_status"=> $apply_status,        // 1申请中2拒绝3通过
        );
    }

    function send_chat_msg($from, $to, $send_message_array, $msgtype)
    {
        // 发入群相关通知
        $this->load->library('curl');

        $message_url = MESSAGE_URL.'v1/sendmsg.json';

        $send_message_string = json_encode($send_message_array);
        $post_message_data = array();
        $post_message_data['fromuid'] = $from; //发送人UID
        $post_message_data['touids'] = $to; //接受人UID
        $post_message_data['message'] = $send_message_string;   //消息体

        $post_message_data['type'] = 0;             //（0系统消息, 1二人对话）
        $post_message_data['apptype'] = self::MSG_APP_TYPE_CHAT;          //分配的apptype
        $post_message_data['msgtype'] = $msgtype;   //消息类型（默认为1别人申请我的群, 2我发出的申请）
        $post_message_data['subject'] = "";         //消息主题（可为空）
        $post_message_data['fromusername'] = "";    //发送者名称（可为空）
        $post_message_data['op'] = 'sendpm';        //发送类型（必须为sendpm）
        //开始发送消息
        $ret = $this->curl->simple_post($message_url,$post_message_data);

        return $ret;
    }

}